package com.cartoonrabbit.controller;


import com.cartoonrabbit.service.IWithdrawOrderService;
import com.cartoonrabbit.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiuTeng
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/withdraw/order")
@Api(tags = "提现相关接口")
public class WithdrawOrderController {

    @Autowired
    private IWithdrawOrderService withdrawOrderService;

    @ApiOperation("用户提现")
    @PreAuthorize("@ss.permitAll()")
    @PostMapping("/transfer")
    public RespBean transfer(Long money, Principal principal) throws IOException {
        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return new RespBean(401,"权限不足，请重新登录",null);
        }
        return withdrawOrderService.transfer(money,principal);
    }

    @ApiOperation("获取提现明细")
    @GetMapping("/list")
    @PreAuthorize("@ss.permitAll()")
    public RespBean getWithdrawOrderList(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                  @RequestParam(value = "size",defaultValue = "10") Integer size,
                                  Principal principal){
        if (ObjectUtils.isEmpty(principal)||ObjectUtils.isEmpty(principal.getName())){
            return RespBean.fail(401,"用户为授权，请重新登录");
        }
        return withdrawOrderService.getWithdrawOrderList(currentPage,size,principal.getName());
    }
}
